import ant,threading,Queue,sys,os,shutil,time
from supplyingant import supplier
from paintingant import painter
from parsingant import parser
from retrievingant import retriever
from wingedant import airborne
import urllib
def lasthelp():
	print("Usage: python py_lastquery.py [commandstring] {[files][,folder]}")
	print("Welcome to Lastquery")
	print("You may choose from various commandstring parameters:")
	print("\"a\" will find album art and album name")
	print("\"g\" will find the most fitting genres")
	print("\"p\" will find album art if album name is known")
	print("\"c\" will conserve information. Lastquery won't touch tagged files")
	print("\"help\" will bring up this dialogue")
	print("")
	print("Simply join the letters you like to form the commandstring")
	print("Currently a g and p are mutually exclusive")
	sys.exit(0)
def start_daemonic_threads(a):
	company=[
		[retriever() for i in range(a)],
		[supplier() for i in range(a)],
		[parser() for i in range(a)],
		[painter() for i in range(a)]
	]
	for agency in company:
		for member in agency:
			member.setDaemon(True) 
			member.start()
	supplier.filelock.acquire()
	try:
		reference=open('ref.txt','r')
	except:
		reference=open('ref.txt','w')
	finally:
		reference.close()
	with open('ref.txt','r') as reference:
		local_content=reference.read()
	supplier.filelock.release()
	while (local_content!=""):
		temp1=local_content.partition("\n")
		temp2=temp1[0].partition("\r")
		supplier.local[temp2[0]]=temp2[2]
		local_content=temp1[2]
def prepare_argument_list(argument):
        if 'temp.m3u'not in argument[2]:
                with open('temp.m3u','w') as f:
                        clear_file=1	
        if (len(argument)>2):							
                if ".mp3" in argument[2]:													#
                        with open('temp.m3u','a') as f:					#
                                for command_line_argument in argument:							#append all arguments passed
                                        if(".py" in command_line_argument): continue
                                        f.write(command_line_argument+"\n")					##
                        return open('temp.m3u','r').readlines()		##R# Return File object for further use
                elif "temp.m3u" in argument[2]:
                        shutil.copy('temp.m3u','temp2.m3u')#if argument is a playlist already
                        a=open('temp.m3u','r').readlines()
                        return a
                elif".m3u" in argument[2]:						#if argument is a playlist already
                        a=open(argument[2],'r').readlines()
                        #print a
                        return a
                elif("." not in argument[2]):
                        supplier.folder=argument[1]
                        liste=list()
                        for file_name in airborne(os.path.abspath(argument[2])):									#	
                                if (".mp3" in file_name)and ("\\fail\\" not in file_name):	#
                                        liste.append(file_name)
                        return liste		##R# Return File object for Further use
                else:
                        print("Invalid parameters")
                        return ""							##R# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        else:										##C# No Arguments passed
                print("This will start recursive autotagging for _all_ files placed within the superior folder")
                print("Did you mean python py_lastquery.py example.mp3 or python lastquery.py example.m3u?")
                print("Do you want to continue?")
                if (raw_input(">>Enter part of name of directory to browse") in os.path.abspath('..')):			#use Airborne method to recursively spot all contained directories											#
                        with open('temp.m3u','a') as f:								#							#	
                                for file_name in airborne(os.path.abspath('..')):								#
                                        if (".mp3" in file_name)and ("\\fail\\" not in file_name):	#
                                                f.write(file_name+"\n")						##
                        return open('temp.m3u','r').readlines()		##R# Return file Object for further use
	
	
